<script setup>
import { ElMessage } from "element-plus";
import { ref, reactive } from "vue";
import router from "@/router";
import { useUser } from "@/store/user.js";
const store = useUser();
const activeName = ref("login");
const loginFormRef = ref();
const loading = ref(false);
const loginForm = reactive({
  mobile: "",
  password: "",
});
const loginRules = reactive({
  mobile: [{ required: true, trigger: "blur", message: "请输入您的账号" }],
  password: [{ required: true, trigger: "blur", message: "请输入您的密码" }],
});
async function handleLogin() {
  try {
    if (activeName.value === "login") {
      console.log(1);
      let res = false;
      store.userList.forEach((e) => {
        const is = isObjectEqual(loginForm, e);
        if (is) {
          res = true;
          return;
        }
      });
      if (!res) {
        loading.value = false;
        ElMessage.error("登录失败，请检查账号密码");
      } else {
        loading.value = false;
        ElMessage.success("登录成功");
        console.log(loginForm);
        if (loginForm.mobile == "admin") {
          router.push("/admin");
        } else {
          router.push("/");
        }
      }
    } else {
      store.$patch({
        userList: [...store.userList, loginForm],
      });
      ElMessage.success("登录成功");
      router.push("/");
    }
  } catch (error) {
    loading.value = false;
  }
}
function isObjectEqual(obj1, obj2) {
  if (Object.keys(obj1).length !== Object.keys(obj2).length) {
    return false;
  }

  for (const key in obj1) {
    if (!obj2.hasOwnProperty(key) || obj1[key] !== obj2[key]) {
      return false;
    }
  }

  return true;
}
</script>

<template>
  <div class="login-container flex justify-center align-center">
    <el-form
      ref="loginFormRef"
      :model="loginForm"
      :rules="loginRules"
      class="login-form"
    >
      <h3 class="title">登录中心</h3>
      <el-tabs v-model="activeName" class="demo-tabs">
        <el-tab-pane label="登录" name="login">
          <el-form-item prop="mobile">
            <el-input
              v-model="loginForm.mobile"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              show-password
            >
            </el-input>
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              type="primary"
              style="width: 100%"
              @click="handleLogin()"
            >
              <span v-if="!loading">登 录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="注册" name="zhuce">
          <el-form-item prop="mobile">
            <el-input
              v-model="loginForm.mobile"
              type="text"
              auto-complete="off"
              placeholder="账号"
            >
            </el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              auto-complete="off"
              placeholder="密码"
              show-password
            >
            </el-input>
          </el-form-item>
          <el-form-item style="width: 100%">
            <el-button
              :loading="loading"
              type="primary"
              style="width: 100%"
              @click="handleLogin()"
            >
              <span v-if="!loading">注册并登录</span>
              <span v-else>登 录 中...</span>
            </el-button>
          </el-form-item>
        </el-tab-pane>
      </el-tabs>
    </el-form>
  </div>
</template>

<style scoped>
.login-container {
  position: absolute;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  background: center/cover url("../assets/login.png");
}
.login-form {
  position: absolute;
  z-index: 2023;
  border-radius: 6px;
  background: #ffffff;
  width: 400px;
  padding: 25px 25px 5px 25px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  font-weight: bold;
  font-size: 30px;
}

.login-footer {
  position: fixed;
  bottom: 0;
  width: 100%;
  height: 40px;
  line-height: 40px;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.el-form-item {
  margin-top: 10px !important;
}
.login {
  width: 100%;
  height: 100%;
  position: absolute;
  left: 0;
  top: 0;
  background: center/cover url("../assets/login.png");
}
</style>
